<template>
  <div>
    <!--顶部工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.username" placeholder="姓名" clearable style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.phone" placeholder="电话" clearable style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.email" placeholder="邮箱" clearable style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="init">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--  展示数据的表格  -->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="100" >
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="140" >
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="140" >
      </el-table-column>
      <el-table-column prop="age" label="年龄" min-width="100" >
      </el-table-column>
      <el-table-column prop="shop_id" label="店铺" min-width="100" >
      </el-table-column>
      <el-table-column prop="department_id" label="部门" min-width="100" >
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100"  >
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper" >
            <el-tag size="medium" v-if="scope.row.state === 1">在职</el-tag>
            <el-tag size="medium" type="warning" v-if="scope.row.state === 0">离职</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" type="info" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--保存界面-->
    <el-dialog :title="saveTitle" :visible.sync="formVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="formRules" ref="saveForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department_id">
          <el-input v-model="saveForm.department_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="shop_id">
          <el-input v-model="saveForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">在职</el-radio>
            <el-radio class="radio" :label="0">离职</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="员工角色">
          <el-select v-model="saveForm.role_id" placeholder="请选择" clearable>
            <!--
               v-for
               :key 用于去重的  一般填id
               :label 显示你选中的值
               :value 提交后给  v-model赋值
            -->
            <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="formVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="listLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--底部工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--   分页查询   -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total" style="float:right">
      </el-pagination>
    </el-col>
  </div>
</template>

<script>
  export default {
    data(){
      return {
        listLoading:false,//忙等
        listData:[],  //装列表数据
        sels:[],      //批量删除使用
        roles:[],     //角色列表
        //高级查询使用的
        filters: {
          phone:'',
          username: '',
          email:''
        },
        //分页需要用的数据
        total:0, //数据的总条数
        currentPage:1, //当前页
        pageSizes:[5, 10, 15, 20],//分页步长
        pageSize:10,        //当前页条数

        //对话框中的数据
        saveForm: {
          id: null,
          username: '',
          state: 1,
          phone:"",
          age:"",
          email:"",
          department_id:"",
          shop_id:"",
          role_id:""
        },
        formVisible : false,//是否显示对话框
        saveTitle : "",//对话框标题
        //required:是否为必填项
        //trigger: 'blur' 失去焦点就验证
        formRules: {
          username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
          phone : [{ required: true, message: '请输入电话', trigger: 'blur' }]
        },
      }
    },
    methods:{
      //给数据前面的选择按钮添加事件
      selsChange(val){
        this.sels = val
      },
      init(){
        this.currentPage = 1
        this.loadListData()
      },
      //远程请求列表数据
      loadListData(){
        let para = {
          currentPage : this.currentPage,
          pageSize : this.pageSize,
          phone : this.filters.phone,
          username : this.filters.username,
          email : this.filters.email
        };
        this.listLoading = true;
        this.$http.post("/emp/pageList",para).then(res=>{
          this.listData = res.data.data;
          this.total = res.data.total;
          this.listLoading = false;
        })
      },
      //当前页发生变化触发的方法
      handleCurrentChange(val){
          this.currentPage = val
          this.loadListData()
      },
      //分页步长发生变化触发的方法
      handleSizeChange(val) {
          this.pageSize = val
          this.loadListData()
      },
      //批量删除
      batchRemove(){
        this.$confirm('确认删除选中的记录吗?', '提示', {
          type: 'warning'
        }).then(() => { //确定
          this.listLoading = true;
          let ids = this.sels.map(item => item.id);//将数组中的每一个对象的id取出来，放入新数组
          this.$http.patch("/emp",ids).then(res=>{
            this.listLoading = false;
            if(res.data.success){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              this.loadListData()
            }else {
              //删除失败 提示信息
              this.$message({
                message: res.data.msg,
                type: 'warning'
              });
            }
          })
        }).catch(() => {  //取消

        });
      },
      //编辑
      handleEdit(row){
        if(this.$refs['saveForm']){
          this.$refs['saveForm'].resetFields();//重置表单验证
        }
        this.formVisible = true
        this.saveTitle = "编辑"
        this.saveForm = Object.assign({},row)
      },
      //删除
      handleDel(row){
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => { //确定
          this.listLoading = true;
          this.$http.delete("/emp/"+row.id).then(res=>{
            this.listLoading = false;
            if(res.data.success){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              this.currentPage = 1  //删除后回到第一页
              this.loadListData()
            }else {
              //删除失败 提示信息
              this.$message({
                message: res.data.msg,
                type: 'warning'
              });
            }
          })
        }).catch(() => {  //取消

        });
      },
      //添加
      add(row){
        if(this.$refs['saveForm']){
          this.$refs['saveForm'].resetFields();//重置表单验证
        }
        this.saveTitle = "新增"
        this.saveForm = {
          id: null,
          username: '',
          state: 1,
          phone:"",
          age:"",
          email:"",
        }
        this.formVisible = true
      },
      //保存方法
      saveSubmit(){
        this.$refs.saveForm.validate((valid) => { //数据验证 结果为true或false
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.listLoading = true;
              let para = Object.assign({}, this.saveForm);
              this.$http.put("/emp",para).then(res=>{
                if(res.data.success) {
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                  this.loadListData()
                  this.formVisible = false;
                }else {
                  this.$message({
                    message: res.data.msg,
                    type: 'warning'
                  });
                  this.listLoading = false;
                }
              })
            });
          }
        });
      },
      //查询全部角色
      getRoles() {
        this.$http.get("role").then(res=>{
          this.roles = res.data
        })
      }
    },

    mounted(){
      //页面加载完毕以后立即执行此方法
      this.loadListData();
      this.getRoles();
    }
  }
</script>

<style scoped>
</style>